package xpn.platform.modules.login;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.web.bind.annotation.*;
import xpn.platform.common.response.ResponseUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

@RepositoryRestController
@RestController
@RequestMapping("/api")
public class LoginController {

	@Autowired
	private LoginService loginService;

	@Value("${outerservice.redirectUrl4A}")
	private String redirectUrl4A;


	/*
	 * JSON登录
	 */
	@PostMapping(value = "login", consumes = "application/json")
	public Object login(@RequestBody LoginRequestDTO loginRequest, HttpServletRequest request,
			PersistentEntityResourceAssembler resourceAssembler) {
		LoginOkDTO dto = (LoginOkDTO) loginService.login(loginRequest.getUsername(), loginRequest.getPassword(),
				request);

		// 设置用户信息的链接
//		dto.getUser().setSelfLink(resourceAssembler.getSelfLinkFor(dto.getSysUser()).getHref());
		PersistentEntityResource link = resourceAssembler.toFullResource(dto.getSysUser());
		return ResponseUtil.ok(dto);
	}

	/*
	 * 表单登录
	 */
	@PostMapping(value = "login", consumes = { "multipart/form-data", "application/x-www-form-urlencoded" })
	public Object formLogin(String userName, String password, HttpServletRequest request,
			PersistentEntityResourceAssembler resourceAssembler) {
		LoginOkDTO dto = (LoginOkDTO) loginService.login(userName, password, request);

		// 设置用户信息的链接
//		dto.getUser().setSelfLink(resourceAssembler.getSelfLinkFor(dto.getSysUser()).getHref());

		return ResponseUtil.ok(dto);
	}

	/*
	 * Get登录：使用用户名和口令
	 */
	@GetMapping("login")
	public Object getLogin(String userName, String password, HttpServletRequest request,
			PersistentEntityResourceAssembler resourceAssembler) {
		LoginOkDTO dto = (LoginOkDTO) loginService.login(userName, password, request);

		// 设置用户信息的链接
//		dto.getUser().setSelfLink(resourceAssembler.getSelfLinkFor(dto.getSysUser()).getHref());

		return ResponseUtil.ok(dto);
	}

	/*
	 * Get登录：使用token
	 */
	@GetMapping("tokenlogin")
	public Object getLogin(String xpnToken, HttpServletRequest request,
			PersistentEntityResourceAssembler resourceAssembler) {
		LoginOkDTO dto = (LoginOkDTO) loginService.login(xpnToken, request);

		// 设置用户信息的链接
//		dto.getUser().setSelfLink(resourceAssembler.getSelfLinkFor(dto.getSysUser()).getHref());

		return ResponseUtil.ok(dto);
	}

	/*
	 * 注销
	 */
	@GetMapping("logout")
	public Object logout() {
		loginService.logout();
		return ResponseUtil.ok("");
	}

	/*
	 * 提供给4A登录认证接口
	 */
	//@PostMapping(value = "authFrom4A", consumes = "application/json")
	@PostMapping(value = "authFrom4A", consumes = { "multipart/form-data", "application/x-www-form-urlencoded" })
	public void authFrom4A(@RequestParam(value="username")String username, @RequestParam(value="password")String password, HttpServletRequest request, HttpServletResponse response,
						   PersistentEntityResourceAssembler resourceAssembler) throws IOException {
		JSONObject ret = (JSONObject)loginService.authFrom4A(username, password,request);
		String url=redirectUrl4A;
		if(ret.containsKey("xpnToken")){
			url+="?xpnToken="+ret.getString("xpnToken");
		}else{
			url+="?errorMsg="+ URLEncoder.encode(ret.getString("errorMsg"),"UTF-8");
		}
		response.sendRedirect(url);
	}
}
